#include <uguithread.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <QTextCodec>
#include <QByteArray>
#include <QList>
#include <QStringList>
#include <stdlib.h>

#define BUFFSIZE  50

static char buff_send[BUFFSIZE] = {0};

UguiThread::UguiThread(QObject *parent) : QThread(parent)
{
    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
        printf("export is error\r\n");
        return;
    }
    printf("fd0: %d \r\n", fd0);
    this->moveToThread(this);
}

void UguiThread::run()
{

}

void UguiThread::WriteText(QString text)
{
//    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
//        printf("export is error\r\n");
//        return;
//    }

//    printf("fd0: %d \r\n", fd0);

    if(fd0 >= 0){

        char *str2  = "          ";
        char buff_send2[50];
        buff_send2[0] = 'a';
        sprintf(buff_send2+1, str2);
        write(fd0,buff_send2,strlen(buff_send2));

        char buff_send[50];
        buff_send[0] = 'a';
        QByteArray ba = text.toLatin1();
        char *str = ba.data();
        sprintf(buff_send+1, str);
        printf("len: %d \r\n", strlen(buff_send));
        write(fd0,buff_send,strlen(buff_send) + 1);
    }

}

void UguiThread::WriteLine(QString name, QString price, int index)
{
//    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
//        printf("export is error\r\n");
//        return;
//    }

//    printf("fd0: %d \r\n", fd0);

    if(fd0 >= 0){

        char lines[5];
        lines[0] = 'b';
        lines[1] = 'c';
        lines[2] = 'd';
        lines[3] = 'e';
        lines[4] = 'f';

        char *str2  = "                                    ";
        char buff_send2[50];
        buff_send2[0] = lines[index];
        sprintf(buff_send2+1, str2);
        write(fd0,buff_send2,strlen(buff_send2));

        char buff_send[50];
        buff_send[0] = lines[index];
        QTextCodec *codec = QTextCodec::codecForName("GBK");
        QByteArray string = codec->fromUnicode(name);
        char *str = string.data();
        sprintf(buff_send+1, str);
        write(fd0,buff_send,strlen(buff_send) + 1);
    }

}

void UguiThread::Clear(){
    if(fd0 >= 0){

    }
}

void UguiThread::WriteAll(QString total, QMap<QString, QString> tabList){

    if(fd0 >= 0){

        QTextCodec *codec = QTextCodec::codecForName("GBK");

        int i=0;
        char *buff_send = malloc(50);
        memset(buff_send,0,50);

        // line1
        buff_send[0] = 'a';

        char *str1  = "          ";
        for(i=0;i<10;i++)
            *(buff_send+i+1)= *(str1+i);
        write(fd0,buff_send,strlen(buff_send));

        if(total.length() > 0)
        {
            memset(buff_send,0,50);
            buff_send[0] = 'a';

            QByteArray ba = total.toLatin1();
            char *str = ba.data();
            for(i=0;i<total.length();i++)
                *(buff_send+i+1)= *(str+i);

            write(fd0,buff_send,strlen(buff_send));
        }

        // line2

        memset(buff_send,0,50);
        buff_send[0] = 'b';

        char *str2  = "                              ";
        for(i=0;i<30;i++)
            *(buff_send+i+1)= *(str2+i);
        write(fd0,buff_send,strlen(buff_send));

        QList<QString> names = tabList.keys();
        char * buf1;

        if(names.length() > 0)
        {
            QString dataline = tabList.value(names.at(names.length() - 1));
            QStringList list = dataline.split(',');
            if(list.count() == 2){
                QString price = list.at(0);
                QString num = list.at(1);
                QString name = names.at(names.length() - 1);

                memset(buff_send,0,50);
                buff_send[0] = 'b';
                QByteArray string = codec->fromUnicode(name);
                buf1 = string.data();

                for(i=0;i<name.length()*2;i++)
                    *(buff_send +i+1)= *(buf1+i);

                write(fd0,buff_send,strlen(buff_send));
            }
        }


        // line3
        memset(buff_send,0,50);
        buff_send[0] = 'c';

        for(i=0;i<30;i++)
            *(buff_send+i+1)= *(str2+i);
        write(fd0,buff_send,strlen(buff_send));

        if(names.length() > 1){
            QString dataline = tabList.value(names.at(names.length() - 2));
            QStringList list = dataline.split(',');
            if(list.count() == 2){
                QString price = list.at(0);
                QString num = list.at(1);
                QString name = names.at(names.length() - 2);

                memset(buff_send,0,50);
                buff_send[0] = 'c';
                QByteArray string = codec->fromUnicode(name);
                buf1 = string.data();

                for(i=0;i<name.length()*2;i++)
                    *(buff_send +i+1)= *(buf1+i);

                write(fd0,buff_send,strlen(buff_send));
            }
        }

        // line4
        memset(buff_send,0,50);
        buff_send[0] = 'd';

        for(i=0;i<30;i++)
            *(buff_send+i+1)= *(str2+i);
        write(fd0,buff_send,strlen(buff_send));

        if(names.length() > 2){
            QString dataline = tabList.value(names.at(names.length() - 3));
            QStringList list = dataline.split(',');
            if(list.count() == 2){
                QString price = list.at(0);
                QString num = list.at(1);
                QString name = names.at(names.length() - 3);

                memset(buff_send,0,50);
                buff_send[0] = 'd';
                QByteArray string = codec->fromUnicode(name);
                buf1 = string.data();

                for(i=0;i<name.length()*2;i++)
                    *(buff_send +i+1)= *(buf1+i);

                write(fd0,buff_send,strlen(buff_send));
            }
        }

        // line5
        memset(buff_send,0,50);
        buff_send[0] = 'e';

        for(i=0;i<30;i++)
            *(buff_send+i+1)= *(str2+i);
        write(fd0,buff_send,strlen(buff_send));

        if(names.length() > 3){
            QString dataline = tabList.value(names.at(names.length() - 4));
            QStringList list = dataline.split(',');
            if(list.count() == 2){
                QString price = list.at(0);
                QString num = list.at(1);
                QString name = names.at(names.length() - 4);

                memset(buff_send,0,50);
                buff_send[0] = 'e';
                QByteArray string = codec->fromUnicode(name);
                buf1 = string.data();

                for(i=0;i<name.length()*2;i++)
                    *(buff_send +i+1)= *(buf1+i);

                write(fd0,buff_send,strlen(buff_send));
            }
        }

        // line6
        memset(buff_send,0,50);
        buff_send[0] = 'f';

        for(i=0;i<30;i++)
            *(buff_send+i+1)= *(str2+i);
        write(fd0,buff_send,strlen(buff_send));

        if(names.length() > 4){
            QString dataline = tabList.value(names.at(names.length() - 5));
            QStringList list = dataline.split(',');
            if(list.count() == 2){
                QString price = list.at(0);
                QString num = list.at(1);
                QString name = names.at(names.length() - 5);

                memset(buff_send,0,50);
                buff_send[0] = 'f';
                QByteArray string = codec->fromUnicode(name);
                buf1 = string.data();

                for(i=0;i<name.length()*2;i++)
                    *(buff_send +i+1)= *(buf1+i);

                write(fd0,buff_send,strlen(buff_send));
            }
        }
    }

}
